$(function() {
	console.log("xxxxxxxxxxx");
	// 查询教室信息
	//请求参数
	let reqParam = {};
	queryData(reqParam);
	findProvinces();//三级联动
});

//条件查询教室信息(单表查询)

function queryClassRoom() {
	//组装参数
	let reqParam = {};
	reqParam.phoneNum = $("#phoneNum").val();
	reqParam.employeeName = $("#employeeName").val();
	reqParam.departmentEmployees = $("#departmentEmployees").val();
	// {"classroomNumber": "123"}
	//调用查询
	queryData(reqParam);

}

/**
 * 查询教室信息
 */
function queryData(reqParam) {

	var url = $("#addr").val() + "/yuangong/queryData";

	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify(reqParam),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//把页面写在html页面上
			//let str = "";
			let arrData = data.data;

			/*	for(let i = 0; i< arrData.length;i++){//JSfor循环
					// arrData[i].createTime获取修改时间
					str = str + "<tr>";
					
					str = str + "<td>"+ arrData[i].userId +"</td>";
					str = str + "<td>"+ arrData[i].sex +"</td>";
					str = str + "<td>"+ arrData[i].phoneNum +"</td>";
					str = str + "<td>"+ arrData[i].employeeName +"</td>";
					str = str + "<td>"+ arrData[i].age +"</td>";
					str = str + "<td>"+ arrData[i].hiredate +"</td>";
					str = str + "<td>"+ arrData[i].email +"</td>";
					str = str + "<td>"+ arrData[i].salary +"</td>";
					str = str + "<td>"+ arrData[i].homeProvince +"</td>";
					str = str + "<td>"+ arrData[i].homeCity +"</td>";
					str = str + "<td>"+ arrData[i].homeArea +"</td>";
					str = str + "<td>"+ arrData[i].workProvince +"</td>";
					str = str + "<td>"+ arrData[i].workCity +"</td>";
					str = str + "<td>"+ arrData[i].workArea +"</td>";
					str = str + "<td>"+ arrData[i].remark +"</td>";
					str = str + "<td>"+ getYMDHMS(arrData[i].createDate) +"</td>";
					str = str + "<td>"+ getYMDHMS(arrData[i].updateDate) +"</td>";
					str = str + "<td>"+ arrData[i].deleteFlag +"</td>";
					
					str = str + "<td>" + " <button type='button' onclick='findData(\"" + userId + "\")' class='btn btn-primary' data-toggle='modal' "
							  + "data-target='#updatemodal' data-whatever='@fat'>修改</button> " + "</td>";
							  
					str = str + "<td>" + " <button type='button'  class='btn btn-primary'  onclick='findData(\"" + userId + "\")'  data-toggle='modal' "
							  + "data-target='#deletemodal' data-whatever='@fat'>删除</button> " + "</td>";          
					str = str + "</tr>";
					
					
					
					
				}*/
			$('#table1').bootstrapTable('destroy');
			$('#table1').bootstrapTable({
				// url: retData,
				data: arrData,
				columns: [{
					field: 'phoneNum',
					title: '手机号'
				}, {
					field: 'employeeName',
					title: '员工姓名'
				}, {
					field: 'age',
					title: '年龄'
				}, {
					field: 'email',
					title: '邮箱'
				}, {
					field: 'departmentEmployees',
					title: '部门'
				}, {
					field: 'hiredate',
					title: '入职时间',
					sortable: true,
					formatter: function(value, row, index) {

						return getYMDHMS(value)
					}
				}, {
					field: 'salary',
					title: '入职薪资'
				}, {
					field: 'homeProvince',
					title: '家庭地址省名称'
				}, {
					field: 'homeCity',
					title: '家庭地址市名称'
				}, {
					field: 'homeArea',
					title: '家庭地址县名称'
				}, {
					field: 'createDate',
					title: '创建时间',
					sortable: true,
					formatter: function(value, row, index) {

						return getYMDHMS(value)
					}
				}, {
					field: 'updateDate',
					title: '修改时间',
					sortable: true,
					formatter: function(value, row, index) {

						return getYMDHMS(value)
					}
				}, {
					field: '',
					title: '操作',
					formatter: function(value, row, index) {//row是data返回的对象，index是返回的长度
						return "<button type='button' onclick='findData(\"" + row.userId + "\")' class='btn btn-primary' data-toggle='modal'  data-target='#updatemodal' data-whatever='@fat'>修改</button>"
							+ "<button type='button' onclick='findData(\"" + row.userId + "\")' class='btn btn-primary' data-toggle='modal' data-target='#deletemodal' data-whatever='@fat'>删除</button>"
					}
				},]
			});
			//引用jsp页面
			//$("#tablebody").html(str);
			// $("#tablebody").append(str);
		}
	});

}
// 状态转换
// 使用状态 0-使用 1-未使用 2-维修中
function useStatus(status) {
	if (status == "0") {
		return "使用";
	} else if (status == "1") {
		return "未使用";
	} else if (status == "2") {
		return "维修中";
	} else {
		return "错误";
	}
}
//时间格式转换
function getYMDHMS(timestamp) {
	let time = new Date(timestamp)
	let year = time.getFullYear()
	let month = time.getMonth() + 1
	let date = time.getDate()
	let hours = time.getHours()
	let minute = time.getMinutes()
	let second = time.getSeconds()

	if (month < 10) { month = '0' + month }
	if (date < 10) { date = '0' + date }
	if (hours < 10) { hours = '0' + hours }
	if (minute < 10) { minute = '0' + minute }
	if (second < 10) { second = '0' + second }
	return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
//新增
function add() {
	var url = $("#addr").val() + "/yuangong/addData";
	var reqParam = {};
	reqParam.userId = $("#userId").val();
	reqParam.employeeName = $("#addemployeeName").val();
	reqParam.age = $("#age").val();
	reqParam.email = $("#email").val();
	reqParam.phonenum = $("#phonenum").val();
	reqParam.homeProvinceid = $("#provinces").find("option:selected").val();
	reqParam.homeProvince = $("#provinces").find("option:selected").text();
	reqParam.homeCityid = $("#city").find("option:selected").val();
	reqParam.homeCity = $("#city").find("option:selected").text();
	reqParam.homeAreaid = $("#areas").find("option:selected").val();
	reqParam.homeArea = $("#areas").find("option:selected").text();
	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify(reqParam),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//引用jsp页面
			$("#addmodal").modal('hide');//关闭模态框
			let reqParam = {};
			queryData(reqParam);
		}
	});
}

//删除
function deleteDatad() {
	var url = $("#addr").val() + "/yuangong/deleteData";
	var reqParam = {};
	reqParam.userId = $("#delete-userId").val();
	reqParam.employeeName = $("#delete-employeeName").val();
	reqParam.age = $("#delete-age").val();
	reqParam.email = $("#delete-email").val();
	reqParam.phonenum = $("#delete-phonenum").val();
	reqParam.homeProvince = $("#delete-homeProvince").val();
	reqParam.homeCity = $("#delete-homeCity").val();
	reqParam.homeArea = $("#delete-homeArea").val();
	reqParam.pkId = $("#pkId").val();
	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify(reqParam),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//引用jsp页面
			$("#deletemodal").modal('hide');//关闭模态框
			let reqParam = {};
			queryData(reqParam);
			
		}
	});
}
//反显userID
function findData(userId) {
	var url = $("#addr").val() + "/yuangong/findByuserId";
	var reqParam = {};
	reqParam.userId = userId;
	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify(reqParam),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			let redata = data.data;
			//反显delete删除页面的数据
			$("#delete-employeeName").val(redata.employeeName);
			$("#delete-age").val(redata.age);
			$("#delete-email").val(redata.email);
			$("#delete-phonenum").val(redata.phoneNum);
			$("#delete-homeProvince").val(redata.homeProvince);
			$("#delete-homeCity").val(redata.homeCity);
			$("#delete-homeArea").val(redata.homeArea);
			$("#pkId").val(redata.pkId);
			//反显update删除页面的数据
			$("#update-userId").val(redata.userId);
			$("#update-employeeName").val(redata.employeeName);
			$("#update-age").val(redata.age);
			$("#update-email").val(redata.email);
			$("#update-phonenum").val(redata.phoneNum);
			$("#update-homeProvince").val(redata.homeProvince);
			$("#update-homeCity").val(redata.homeCity);
			$("#update-homeArea").val(redata.homeArea);
			$("#pkId").val(redata.pkId);
		}
	});
}
//修改
function update() {
	var url = $("#addr").val() + "/yuangong/updateData";
	var reqParam = {};
	reqParam.userId = $("#update-userId").val();
	reqParam.employeeName = $("#update-employeeName").val();
	reqParam.age = $("#update-age").val();
	reqParam.email = $("#update-email").val();
	reqParam.phonenum = $("#update-phonenum").val();
	reqParam.homeProvince = $("#update-homeProvince").val();
	reqParam.homeCity = $("#update-homeCity").val();
	reqParam.homeArea = $("#update-homeArea").val();
	reqParam.pkId = $("#pkId").val();
	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify(reqParam),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//引用jsp页面
			$("#updatemodal").modal('hide');//关闭模态框
			let reqParam = {};
			queryData(reqParam);
		}
	});
}


/*

三级联动


*/
function findProvinces() {
	var url = $("#addr").val() + "/yuangong/findProvinces";

	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify({}),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			console.log("findProvinces成功 ");
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			var redata = data.data;
			var str = "<option value=''>请选择省</option>";

			for (let i = 0; i < redata.length; i++) {
				str = str + "<option value='" + redata[i].provinceid + "'>" + redata[i].province + "</option>";
			}
			$("#provinces").html(str);
		}
	})
}

function findCities() {
	console.log("findCities");

	let provinceId = $("#provinces").find("option:selected").val();
	queryCities(provinceId);
	console.log("provinceIdasdsadasd " + provinceId);

	var str = "<option value=''>请选择市</option>";
	$("#areas").html(str);
}
function queryCities(provinceId) {

	var url = $("#addr").val() + "/yuangong/findCities";
	let arr = {};
	arr.provinceId = provinceId;
	$.ajax({
		type: "POST",
		url: url,
		data: JSON.stringify(arr),
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		async: true,
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			console.log("queryCities成功 ");
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			var redata = data.data;
			var str = "<option value=''>请选择市</option>";

			for (let i = 0; i < redata.length; i++) {
				str = str + "<option value='" + redata[i].cityid + "'>" + redata[i].city + "</option>"
			}
			$("#city").html(str);
		}
	})
}
function findAreas() {

	console.log("findAreas");

	let cityid = $("#city").find("option:selected").val();

	queryAreas(cityid)
}
function queryAreas(cityid) {
	var url = $("#addr").val() + "/yuangong/findAreas";
	let arr = {};
	arr.cityid = cityid;
	$.ajax({
		type: "POST",
		url: url,
		data: JSON.stringify(arr),
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		async: true,
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			console.log("queryAreas成功 ");
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			var redata = data.data;
			var str = "<option value=''>请选择县</option>";
			for (let i = 0; i < redata.length; i++) {

				str = str + "<option value='" + redata[i].area + "'>" + redata[i].area + "</option>"
			}
			$("#areas").html(str);

		}
	})
}



